﻿#region "Copyright (C) Lenny Granovsky. 2011-2013"
//    This library provides extended cache capabilities to the ASP.NET applications.
//
//                Copyright (C) Lenny Granovsky. 2011-2013. 
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    This program comes with ABSOLUTELY NO WARRANTY.
//    This is free software, and you are welcome to redistribute it
//    under certain conditions;
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlexiCache
{
    /// <summary>
    /// Class is responsible for instantiating cache storage adapters based on current configuration.
    /// </summary>
    internal class CacheStorageAdapterFactory
    {
        /// <summary>
        /// Method creates an instance of cache storage adapter based on configuration.
        /// </summary>
        /// <param name="config">Config parameters.</param>
        /// <returns>Returns an instance of cache storage adapter.</returns>
        internal static IExternalCacheStorageAdapter CreateInstance(CacheStorageConfigInfo config)
        {
            if (config.IsEnabled)
            {
                IExternalCacheStorageAdapter adapter = (IExternalCacheStorageAdapter)Activator.CreateInstance(config.AdapterType);
                adapter.IsEnabled = config.IsEnabled;
                adapter.CacheDatabaseName = config.CacheDatabaseName;
                adapter.IsAcknowledgedWrite = config.IsAcknowledgedWrite;
                adapter.MaxDataSizeLength = config.MaxDataSizeLength;
                adapter.TracePrefix = config.TracePrefix;
                adapter.UseCompression = config.UseCompression;
                adapter.DBConnectionString = config.DBConnectionString;
                return adapter;
            }
            return UnavailableCacheStorageAdapter.Empty;
        }
    }
}
